<?php


namespace app\api\controller\v1;


use app\admin\model\BdlCarouselchart;
use app\admin\model\Customer;
use app\admin\model\Inventory;
use app\admin\model\Materialimage;
use app\admin\model\Notice;
use app\admin\model\Surface;
use app\common\model\Config;
use think\Cache;
use think\Env;

class Index extends Base
{
    public function index()
    {
        $cacheTag       = \config('cache_tag')['v1_index_index'];
        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . json_encode($this->forgids);
        $data         =  Cache::get($cacheKey);

        if(!$data)
        {
            // 获取轮播图
            $data = BdlCarouselchart::where('fenable', 1)
                ->where('forgid','in', $this->forgids)
                ->where('fk_bdl_bd_datacenter', $this->datacenter)
                ->order('fk_bdl_weight', 'asc')
                ->select()
                ->each(function($a){if($a['fk_bdl_jumptype'] == 1){if(!containsString($a['fk_bdl_url'],'http')){$a['fk_bdl_jumptype'] = 3;}}});

            Cache::tag($cacheTag)->set($cacheKey,$data,$this->cacheExpire);
        }


        //未登录展示
        if(!$this->user_id)
        {
            $show_data = [
                'show_logo'       => Config::where('name','notLoginShowLogo')->value('value'),
                'show_titurl'     => Config::where('name','notLoginShowTitle')->value('value'),
                'show_welcome'    => '',
                'show_tel'        => $this->getPhone()
            ];

        }else{
            $customer = Customer::where('id',$this->customer_id)->find();
            $show_data = [
                'show_logo'       => Config::where('name','loginShowLogo')->value('value'),
                'show_titurl'     => Config::where('name','loginShowTitle')->value('value'),
                'show_welcome'    => !empty($customer['fname']) ? $customer['fname'] : '',
                'show_tel'        => $this->getPhone()
            ];
        }

        $notice        = Notice::where('fenable',1)->where('fk_bdl_isindex',1)->order('id','desc')->field('id,fenable,fk_bdl_title')->select();


        $this->success('请求成功', [
            'banner' => $data,
            'show_data' => $show_data,
            'notice'    => $notice
        ]);
    }
    public function getNotice()
    {
        $notice        = Notice::where('fenable',1)->order('id','desc')->field('id,fenable,fk_bdl_title')->select();

        $this->success('请求成功', $notice);
    }
    public function noticeDetail()
    {
        $id = $this->params['id'];
        if(empty($id))
        {
            $this->error('参数错误');
        }

        $data = Notice::where('fenable',1)->where('id',$id)->find();


        $this->success('请求成功', $data);
    }

      //现货产品
    public function material()
    {
        $page = $this->params['page'] ?? 1;

        $cacheTag       = \config('cache_tag')['v1_index_material'];
        if(!$this->user_id)
        {
            $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . json_encode($this->forgids) . $page;
        }else{
            $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . json_encode($this->forgids) . $page . md5($this->datacenter);
        }

        $data           =  Cache::get($cacheKey);

        if(!$data)
        {
            //现货库存产品
            $data = Inventory::with(['inventoryentryvalue','mateia','surfaceone','textureone'])
                ->where('fenable', '1')
                ->whereIn('forgid', $this->forgids)
                ->where('fk_bdl_bd_datacenter', (string)$this->datacenter)
                ->where('fk_bdl_usableqty', '>', 0)
                ->order('id', 'desc')
                ->page($page, $this->pageSize)
                ->whereNotNull('fk_bdl_bd_material')
                ->whereNotNull('fk_bdl_inventoryjsondata')
                ->select()
                ->each(function($item) {
                    $item->show_mes     = $this->getInventoryStatusMessage($item->fk_bdl_usableqty);
                    //拿到长宽厚
                    $show = [];
                    foreach ($item->inventoryentryvalue as $key => $val)
                    {
                        if(in_array($val['fnumber'],['bdl_thickness','bdl_width','bdl_length','bdl_refthickness','bdl_materialgrade']))
                        {
                                $show[$val['fnumber']] = $val['pivot']['fk_bdl_matpropvalue'];
                        }
                    }

                    $surface_id     = $item['surfaceone']['id'];
                    $material_id    = $item['mateia']['id'];
                    if(!empty($surface_id) && !empty($material_id))
                    {
                        $find       = Materialimage::where('fk_bdl_bd_surface',$surface_id)->where('fk_bdl_bd_material',$material_id)->find();
                        if(!empty($find))
                        {
                            $item['show_image'] = $find['fk_bdl_imageurl'];
                        }else{
                            $item['show_image'] = $this->getDefaultImage();
                        }
                    }else{
                        $item['show_image'] = $this->getDefaultImage();
                    }

                    $item->show_matprop = $show;
                });

            Cache::tag($cacheTag)->set($cacheKey,$data,$this->cacheExpire);
        }


        $this->success('请求成功' . count($data), $data);
    }


    public function getPhone()
    {
        if(!$this->user_id)
        {
            $phone          = Config::where('name','phone_sys')->value('value');
        }else{
            $customer       = Customer::with(['bizperson'])->find($this->customer_id);

            $phone          = strlen($customer['bizperson']['fk_bdl_phone']) !== 0 && $customer['bizperson']['fk_bdl_phone'] !== ' '  ? $customer['bizperson']['fk_bdl_phone']: Config::where('name','phone_sys')->value('value');
        }


        return $phone;
    }




}